package com.bk.service.loan;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bk.entity.CardInfo;
import com.bk.entity.LoanBalance;
import com.bk.entity.LoanInfo;
import com.bk.entity.UserInfo;
import com.bk.mapper.cardInfo.CardInfoMapper;
import com.bk.mapper.loan.LoanInfoMapper;
import com.bk.mapper.userInfo.UserInfoMapper;
import com.bk.util.GeneratorCardIdUtil;
import com.bk.util.ReturnUtil;
import com.bk.util.TimeUtil;
import com.bk.vo.LoanInfoUserVo;

@Service
public class LoanInfoService {

	@Autowired
	private UserInfoMapper userInfoMapper;

	@Autowired
	private CardInfoMapper cardInfoMapper;
	
	@Autowired
	private LoanInfoMapper loanInfoMapper;

	/**
	 * @desc 查询信用级别 
	 * @param uid
	 * @param name
	 * @param repaymentAccountId  还款账户
	 * @return  用户包含了信用级别
	 */
	public ReturnUtil<UserInfo> queryCreditByUid(String uid, String name,String repaymentAccountId) {
		List<UserInfo> userList = new ArrayList<UserInfo>();
		UserInfo userInfo = userInfoMapper.isExistUserInfo(uid, name);
		if (userInfo != null) {
			CardInfo existCardByAccountId = cardInfoMapper.isExistCardInfo(uid, repaymentAccountId);
			if(null!=existCardByAccountId) {
				userList.add(userInfo);
				return userList.size() > 0 ? new ReturnUtil<UserInfo>(200, "已查询到信用级别", userList)
						: new ReturnUtil<UserInfo>(400, "未能正确查询到信用级别", null);
			}else {
				return new ReturnUtil<UserInfo>(400, "贷款人还款账户不存在或已冻结", null);
			}
			
		} else {
			return new ReturnUtil<UserInfo>(400, "贷款人身份证与账户名不匹配", null);
		}
	}

	/**贷款申请*/
	public ReturnUtil<String> approvalLoan(LoanInfo loanInfo) {
		String dateTime=TimeUtil.getCurrentTimeUtil();
		loanInfo.setLoanState(0);
		loanInfo.setLoanDateTime(dateTime);
		LoanInfo loan=loanInfoMapper.isExistLoanAccount(loanInfo.getAccountUid());//判断是否已有贷款账户从贷款余额表
		if(null!=loan) {//非第一次贷款申请
			loanInfo.setLoanAccountId(loan.getLoanAccountId());
			int approvalLoancode=loanInfoMapper.approvalLoan(loanInfo);//向贷款记录表记录贷款申请
			int recode=loanInfoMapper.updateLoanBalance(loan.getLoanAccountId(),loanInfo.getLoanBalance());//贷款余额表给贷款账户加值
			return recode>0&&approvalLoancode>0?new ReturnUtil<String>(200, "","贷款申请提交成功"):new ReturnUtil<String>(400,"", "贷款申请提交成功");
		}else {//第一次贷款申请
			int recode=loanInfoMapper.approvalLoan(loanInfo);//向贷款记录表记录贷款申请
			return recode>0?new ReturnUtil<String>(200, "","贷款申请提交成功"):new ReturnUtil<String>(400,"", "贷款申请提交成功");
		}
	}

	/**获取所有贷款申请*/
	public ReturnUtil<LoanInfo> getAllWaitPassLoanInfo() {
		List<LoanInfo> loanInfoList=loanInfoMapper.getAllWaitPassLoanInfo();
		return loanInfoList.size()>0?new ReturnUtil<LoanInfo>(200,"查询到所有贷款申请",loanInfoList):new ReturnUtil<LoanInfo>(400,"没有查询到贷款申请",loanInfoList);
	}


	/**贷款审批通过还是驳回*/
	public ReturnUtil<String> isPassOrRefuseLoan(String uid, int loanNum, int flag) {
		if(flag==1) {//审批通过
			LoanInfo loan=loanInfoMapper.queryLoanInfoByLoanNum(loanNum);//通过贷款序列查询贷款记录
			LoanBalance loanBalance=loanInfoMapper.queryLoanBalanceByUid(uid);//查看贷款账户余额表中是否有贷款账户
			if(null!=loanBalance) {//存在贷款账户  贷款账户非第一次审批
				int loanBalanceCode=loanInfoMapper.updateLoanBalance(loanBalance.getLoanAccountId(),loan.getLoanBalance());//更新贷款账户余额
				int loanInfoCode=loanInfoMapper.updateLoanInfoStatuAndLoanAccId(uid,loanBalance.getLoanAccountId(),loanNum,flag);//修改贷款信息表记录中审批状态以及补充贷款账户
				return loanBalanceCode>0&&loanInfoCode>0?new ReturnUtil<String>(200, "","贷款审批成功"):new ReturnUtil<String>(400, "","贷款审批失败");
			}else {//不存在  贷款账户第一次审批
				String loanAccountId=GeneratorCardIdUtil.get_Bank_account();
				int code=loanInfoMapper.createLoanBalanceAccId(uid,loanAccountId,loan.getLoanBalance());//创建贷款账户,并赋予初值
				int loanInfoCode=loanInfoMapper.updateLoanInfoStatuAndLoanAccId(uid,loanAccountId,loanNum,flag);//修改贷款信息表记录中审批状态以及补充贷款账户
				return code>0&&loanInfoCode>0?new ReturnUtil<String>(200, "","贷款审批成功"):new ReturnUtil<String>(400, "","贷款审批失败");
			}
		}else {
			int loanInfoCode=loanInfoMapper.updateLoanInfoStatuAndLoanAccId(uid,null,loanNum,flag);//修改贷款信息表记录中审批状态以及补充贷款账户
			return loanInfoCode>0?new ReturnUtil<String>(200, "","贷款审批驳回成功"):new ReturnUtil<String>(400, "","贷款审批驳回失败");
		}
		
	}

	/**获取所有已驳回申请*/
	public ReturnUtil<LoanInfo> getAllNoPassLoanInfo() {
		List<LoanInfo> loanInfoList=loanInfoMapper.getAllNoPassLoanInfo();
		return loanInfoList.size()>0?new ReturnUtil<LoanInfo>(200,"查询到所有已驳回申请",loanInfoList):new ReturnUtil<LoanInfo>(400,"没有查询到已驳回申请",loanInfoList);
	}

}
